package com.kayak.model
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	
	public class HotelSearch extends EventDispatcher
	{
		public var searchQuerryResults		: int = 5;
		public var totalGuests					: int = 1;
		public var totalRooms					: int = 1;
		
		public var kayakApiKey					: String;
		public var checkInDate					: String;
		public var checkOutDate				: String;
		public var hotelLocation					: String;
		
		private const SessionLoader			: URLLoader = new URLLoader ();
		private const ResultLoader			: URLLoader = new URLLoader ();
		private const SearchLoader			: URLLoader = new URLLoader ();
		
		private const SessionRequest			: URLRequest = new URLRequest ( 'http://api.kayak.com/k/ident/apisession' );
		private const ResultRequest			: URLRequest = new URLRequest ( 'http://api.kayak.com/s/apisearch' );
		private const SearchRequest			: URLRequest = new URLRequest ( 'http://api.kayak.com/s/apibasic/hotel' );
		
		private var SessionXML					: XML;
		private var ResultXML					: XML;
		private var QueryXML					: XML;
		private var SearchXML					: XML;
		
		/**
		 * Constructor.
		 **/
		public function HotelSearch ()
		{
			super ( null );
			
			this.SessionLoader.dataFormat = URLLoaderDataFormat.TEXT;
			this.SessionLoader.addEventListener ( Event.COMPLETE, onSession );
			
			this.ResultLoader.dataFormat = URLLoaderDataFormat.TEXT;
			this.ResultLoader.addEventListener ( Event.COMPLETE, onResult );
			
			this.SearchLoader.dataFormat = URLLoaderDataFormat.TEXT;
			this.SearchLoader.addEventListener ( Event.COMPLETE, onSearch );
		}
		
		/**
		 * Returns the flight search result
		 * @return	XML
		 **/
		public function get Results () : XML 
		{
			return this.SearchXML;
		}
		
		/**
		 * Execute flight search
		 **/
		public function Search () : void 
		{
			var vars : URLVariables	= new URLVariables ();
			vars.token 					= this.kayakApiKey;
			vars.version 					= '1';
			
			this.SessionRequest.data = vars;
			this.SessionRequest.method = URLRequestMethod.GET;
			this.SessionLoader.load ( this.SessionRequest );
		}
		
		// handle session complete
		private function onSession ( e : Event ) : void 
		{
			this.SessionXML 			= new XML ( e.target.data );
			
			var vars : URLVariables	= new URLVariables ();
			vars.basicmode 				= 'true';
			vars.apimode 				= '1';
			vars.othercity 				= this.hotelLocation;
			vars.checkin_date 			= this.checkInDate;
			vars.checkout_date			= this.checkOutDate;
			vars.guests1 					= this.totalGuests.toString ();
			vars.rooms 					= this.totalRooms.toString ();
			vars.action 					= 'dohotels';
			vars._sid_ 						= this.SessionXML.sid;
			vars.version 					= '1';
			
			this.ResultRequest.data = vars;
			this.ResultRequest.method = URLRequestMethod.GET;
			this.ResultLoader.load ( this.ResultRequest );
		}
		
		// handle result complete
		private function onResult ( e : Event ) : void
		{
			this.ResultXML 		= new XML ( e.target.data );
			
			var vars : URLVariables	= new URLVariables ();
			vars.searchid 				= this.ResultXML.searchid;
			vars._sid_ 						= this.SessionXML.sid;
			vars.c 							= this.searchQuerryResults.toString ();
			vars.m 							= 'normal';
			vars.d							= 'up';
			vars.s 							= 'price';
			vars.version 					= '1';
			vars.apimode 				= '1';
			
			this.SearchRequest.data = vars;
			this.SearchRequest.method = URLRequestMethod.GET;
			this.SearchLoader.load ( this.SearchRequest );
		}
		
		// handle flight search results.
		private function onSearch ( e : Event ) : void 
		{
			this.SearchXML = new XML ( e.target.data );
			this.dispatchEvent ( new KayakEvents ( KayakEvents.SEARCH_READY ) ); 
		}
	}
}